import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';

import { orderStateArrs, loveRecycGivestatusArrs } from '../../love-recyc-manage.pipe';
import { CommonService } from '../../../../services-common/common.service';
import { LoveSendIdolManageService } from '../love-send-idol-manage.service';

@Component({
  selector: 'app-love-send-idol-details',
  templateUrl: './love-send-idol-details.component.html',
  styleUrls: ['./love-send-idol-details.component.scss']
})
export class LoveSendIdolDetailsComponent implements OnInit {

  routeParams: any = {};
  recordSearchOptions = [
    { type: 1, labelText: '订单编号', valueKey: 'okey', value: '' },
    {
      type: 2,
      labelText: '用户身份',
      valueKey: 'userType',
      value: [],
      multiple: false,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [
        {value: 0, label: '游客'},
        {value: 1, label: '爱思用户'}
      ]
    },
    {
      type: 2,
      labelText: '订单状态',
      valueKey: 'orderState',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'id', label: 'label' },
      selectOptions: [...orderStateArrs].filter(m => ![11, 13, 14, 20].includes(m.id))
    },
    {
      type: 2,
      labelText: '赠送状态',
      valueKey: 'state',
      value: [],
      multiple: false,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...loveRecycGivestatusArrs]
    },
    { type: 3, labelText: '下单时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true }
  ];
  recordSearchParams: any = {};

  conditionOptionsC: Array<any> = [];

  activityInfoData: any = {}; // 活动信息
  activityCensusData: any = {}; // 活动统计

  recordTableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 10,
    total: 0
  };

  constructor(
    private msg: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private commonService: CommonService,
    private services: LoveSendIdolManageService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};
    })
  }

  ngOnInit(): void {
    this.loadConditionData();
    this.loadDetailData();
  }

  // 指定C端成色
  async loadConditionData() {
    this.conditionOptionsC = await this.commonService.getDictList('machine_condition');
  }

  async loadDetailData() {
    let params = {
      id: this.routeParams.id
    }
    const messId = this.msg.loading('数据加载中..', { nzDuration: 0 }).messageId;
    const {code, data, message}: any = await this.services.getLoveActSendIdolDetails(params);
    this.msg.remove(messId);
    
    if (code === 0) {
      const { census, recycleActivityIdol } = data || {};
      this.activityInfoData = recycleActivityIdol || {}; // 活动信息
      this.activityCensusData = census || {}; // 活动统计
    }
  }

  async loadRecordTbaleData() {
    let params = {
      activityId: this.routeParams.id,
      ...this.recordSearchParams,
      page: this.recordTableParams.page,
      pageSize: this.recordTableParams.pageSize
    }
    this.recordTableParams.loading = true;
    const {code, data, message}: any = await this.services.getLoveIdolSendRecordList(params);
    this.recordTableParams.loading = false;

    if (code === 0) {
      const {records, total} = data || {};
      this.recordTableParams.tableData = records || [];
      this.recordTableParams.total = total || 0;
    }
  }

  queryResetRecordData(params: any) {
    this.recordSearchParams = params;
    this.recordTableParams.page = 1;
    this.loadRecordTbaleData();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.recordTableParams.page = index;
    this.loadRecordTbaleData();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.recordTableParams.page = 1;
    this.recordTableParams.pageSize = sizeNum;
    this.loadRecordTbaleData();
  }

  setDictText(arrs: Array<any>, arrKey: any, tVal: any) {
    const valArrs = tVal ? tVal.split(',') : [];
    let strArrs: Array<any> = [];

    if (arrs.length && valArrs.length) {
      arrs.forEach(item => {
        if (valArrs.includes(String(item[arrKey]))) {
          strArrs.push(item.name);
        }
      })
    }
    return tVal == '0' ? '全部' : strArrs.join('、');
  }
}
